package com.lovo.pz.window;

import com.lovo.pz.util.GameUtil;
import com.lovo.pz.util.UserBean;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Collections;

public class MenuPanel extends JPanel implements MouseMotionListener, MouseListener {
    private MainFrame mainFrame;
    private int startGameImageIndex = 0;
    private int miniGameImageIndex = 0;
    public MenuPanel(MainFrame mainFrame){
        this.mainFrame = mainFrame;
        this.addMouseMotionListener(this);
        this.addMouseListener(this);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(GameUtil.titleImage,0,0,this);

        g.drawImage(GameUtil.startGameImage[startGameImageIndex],470, 55, 331, 146,this );
        g.drawImage(GameUtil.miniGameImage[miniGameImageIndex],470, 160, 310, 131,this);
        g.drawImage(GameUtil.decodeGameImage[0],475, 242, 290, 131,this);
        g.drawImage(GameUtil.liveImage[0],475, 318, 270, 131,this);

        g.drawImage(GameUtil.rankImage[0],645, 490,this);
        g.drawImage(GameUtil.helpImage[0],734, 523,this);
        g.drawImage(GameUtil.exitImage[0],815, 512,this);

        g.drawImage(GameUtil.showNameImage,20,0,this);
        g.drawImage(GameUtil.switchNameImage[0],20,140,this);

        if(mainFrame.getUserBean() != null){
            // 在左上角显示玩家姓名
            g.setFont(new Font("黑体",Font.BOLD,20));
            g.setColor(Color.WHITE);
            g.drawString(mainFrame.getUserBean().getName(),140,115);
        }
    }

    @Override
    public void mouseDragged(MouseEvent e) {

    }

    @Override
    public void mouseMoved(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        if(x >= 475 && x <= 790 && y >= 80 && y <= 190){
            startGameImageIndex = 1;
        }else{
            startGameImageIndex = 0;
        }
        if(x >= 470 && x <= 780 && y >= 160 && y <= 291){
            miniGameImageIndex = 1;
        }else{
            miniGameImageIndex = 0;
        }
        this.repaint();
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        if(x >= 475 && x <= 790 && y >= 80 && y <= 190){
            if(mainFrame.getUserBean() == null){
                String name = JOptionPane.showInputDialog("请输入玩家姓名：");
                if(name != null && !"".equals(name)){
                    mainFrame.setUserBean(new UserBean(name));
                    mainFrame.getUsers().add(mainFrame.getUserBean());
                    mainFrame.switchPanel("game");
                    mainFrame.getGamePanel().startGame();
                }else{
                    JOptionPane.showMessageDialog(this,"必须输入玩家姓名才能开始游戏");
                }
            }else{
                mainFrame.getUserBean().setScore(0);
                mainFrame.switchPanel("game");
                mainFrame.getGamePanel().startGame();
            }
        }

        if(x > 49 && x < 289 && y > 155 && y < 174){
            String name = JOptionPane.showInputDialog(this,"请输入玩家姓名：");
            if(name != null && !"".equals(name)){
                mainFrame.setUserBean(new UserBean(name));
                mainFrame.getUserBean().setName(name);
                mainFrame.getUsers().add(mainFrame.getUserBean());
            }

        }
        if(x > 645 && x < 726 && y > 490 && y < 521){
            // 讲玩家集合进行排序
            Collections.sort(mainFrame.getUsers());
//            System.out.println(mainFrame.getUsers());
            mainFrame.switchPanel("rank");
        }
        this.repaint();
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
